package fu.capstone.hanoicityguide.dataaccess.holiday;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.Holiday;
import fu.capstone.hanoicityguide.dataaccess.base.BaseDAOImpl;
import fu.capstone.hanoicityguide.dataaccess.base.DatabaseHelper;

public class HolidayDAOImp extends BaseDAOImpl implements HolidayDAO {

	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private Context context;

	public HolidayDAOImp(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	@Override
	public ArrayList<Holiday> listHoliday(String keyword, String sorter) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		ArrayList<Holiday> arrResult = new ArrayList<Holiday>();
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryHoliday = "SELECT Holiday.[Id], Holiday.[Name], Holiday.[Description], "
					+ "AlbumImage.[IconPath] FROM Holiday "
					+ "LEFT JOIN AlbumImage ON Holiday.[AlbumImageId] = AlbumImage.[Id]";

			// filter by keyword
			queryHoliday = queryHoliday + "WHERE Holiday.[Name] LIKE '%"
					+ keyword + "%' ";

			// sort
			if (sorter.equals(context.getResources().getStringArray(
					R.array.sorter_items)[2])) {
				queryHoliday = queryHoliday + "ORDER BY Holiday.[Name] DESC";
			} else if (sorter.equals(context.getResources().getStringArray(
					R.array.sorter_items)[3])) {
				queryHoliday = queryHoliday + "ORDER BY Holiday.[Name] ASC";
			}

			cursor = database.rawQuery(queryHoliday, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						Holiday holiday = new Holiday();
						holiday.setId(cursor.getInt(cursor.getColumnIndex("Id")));
						holiday.setName(cursor.getString(cursor
								.getColumnIndex("Name")));
						holiday.setIconImage(cursor.getString(cursor
								.getColumnIndex("IconPath")));
						arrResult.add(holiday);
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query DB", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return arrResult;

	}

	@Override
	public Holiday browseHoliday(int holidayID) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		Holiday holiday = new Holiday();
		ArrayList<String> arrImage = new ArrayList<String>();

		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryFood = "SELECT Holiday.[Id], Holiday.[Name], Holiday.[Description], "
					+ "AlbumImage.[ImagePath1], AlbumImage.[ImagePath2], AlbumImage.[ImagePath3], "
					+ "AlbumImage.[ImagePath4], AlbumImage.[ImagePath5] FROM Holiday "
					+ "LEFT JOIN AlbumImage ON Holiday.[AlbumImageId] = AlbumImage.[Id] "
					+ "WHERE Holiday.[Id] == " + holidayID;

			cursor = database.rawQuery(queryFood, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						holiday.setId(cursor.getInt(cursor.getColumnIndex("Id")));
						holiday.setName(cursor.getString(cursor
								.getColumnIndex("Name")));
						holiday.setDesc(cursor.getString(cursor
								.getColumnIndex("Description")));

						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath1")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath2")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath3")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath4")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath5")));
						holiday.setArrImage(arrImage);

					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query DB", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return holiday;
	}

}
